package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.yueyoga.admin.dao.DiscountStrategyDAO;
import org.millioncall.yueyoga.admin.model.DiscountStrategy;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.service.DiscountService;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

/**
 * 折扣策略业务实现类
 * 
 * @author zangjinyu
 * @version 1.1.0
 * @since 1.1.0<br>
 *        Last modified by zangjinyu on 2014-3-15
 * 
 * 
 */
public class DiscountServiceImpl implements DiscountService {
	// Aviator表达式中的变量名称，会员使用member表示，当前时间用datetime表示
	private static final String EXPRESSION_VAR_MEMBER = "member";
	private static final String EXPRESSION_VAR_SYSDATE = "datetime";

	private DiscountStrategyDAO discountStrategyDAO;

	public DiscountStrategyDAO getDiscountStrategyDAO() {
		return discountStrategyDAO;
	}

	public void setDiscountStrategyDAO(DiscountStrategyDAO discountStrategyDAO) {
		this.discountStrategyDAO = discountStrategyDAO;
	}

	/**
	 * 查询所有产品策略
	 * 
	 * @param productId
	 * @return
	 */
	private List<DiscountStrategy> findDiscountStrategiesByProductId(
			String productId) {
		List<DiscountStrategy> allValidStragegies = new ArrayList<DiscountStrategy>();

		List<DiscountStrategy> allProductStragegies = this.discountStrategyDAO
				.findByProductId(productId);
		if (!allProductStragegies.isEmpty()) {
			allValidStragegies.addAll(allProductStragegies);
		}

		List<DiscountStrategy> allFreeStragegies = this.discountStrategyDAO
				.findAllFreeStrategies();
		if (!allFreeStragegies.isEmpty()) {
			allValidStragegies.addAll(allFreeStragegies);
		}
		return allValidStragegies;
	}

	/**
	 * 查询所有订单折扣策略
	 * 
	 * @return
	 */
	private List<DiscountStrategy> findAllOrderDiscountStrategies() {
		return this.discountStrategyDAO.findAllOrderStragegies();
	}

	/**
	 * 从一个折扣策略列表中获取匹配的折扣策略
	 * 
	 * @param stragegies
	 * @param buyer
	 * @return
	 */
	private List<DiscountStrategy> matchDiscountStragegies(
			List<DiscountStrategy> stragegies,
			Map<String, Object> discountContext) {
		List<DiscountStrategy> rtn = new ArrayList<DiscountStrategy>();
		Map<String, Object> params = new HashMap<String, Object>();

		Member buyer = (Member) discountContext.get(EXPRESSION_VAR_MEMBER);

		params.put(EXPRESSION_VAR_MEMBER, buyer);
		params.put(EXPRESSION_VAR_SYSDATE, new Date());

		for (DiscountStrategy strategy : stragegies) {
			Expression compiledExp = AviatorEvaluator.compile(strategy
					.getAviatorExpression().getExpression(), true);
			Boolean result = (Boolean) compiledExp.execute(params);
			if (result.booleanValue()) {
				rtn.add(strategy);
			}
		}
		return rtn;
	}

	/**
	 * 查询所有产品折扣策略
	 */
	@Override
	public List<DiscountStrategy> findAllValidDiscountStrategiesByProductId(
			String productId, Map<String, Object> discountContext) {
		List<DiscountStrategy> all = findDiscountStrategiesByProductId(productId);
		return matchDiscountStragegies(all, discountContext);
	}

	/**
	 * 查询所有订单折扣策略
	 */
	@Override
	public List<DiscountStrategy> findAllValidOrderDiscountStrategies(
			Map<String, Object> discountContext) {
		List<DiscountStrategy> all = findAllOrderDiscountStrategies();
		return matchDiscountStragegies(all, discountContext);
	}

}
